const { remote, app } = require('electron');

const Store = require('electron-store')

const store = new Store({name:'savedFileLocation'});

const $ = (selector) => {
  return document.querySelector(selector)
}

const fileLocationSaveInStore = (path) => {
  store.set('savedFileLocation',path);
}

//dom加载完毕之后
document.addEventListener('DOMContentLoaded',() => {
  const storeFileLocation = store.get('savedFileLocation');
  if (storeFileLocation) {
    $('#save-file-location').value = storeFileLocation;
  } else {
    $('#save-file-location').value = app.getPath('desktop')
  }

  $('#select-new-location').addEventListener('click',() => {
    remote.dialog.showOpenDialog({
      title:'请选择存储位置',
      properties:['openDirectory']
    }).then(result => {
      console.log('result',result);
      if(!result.canceled) {
        const filePaths = result.filePaths;
        $('#save-file-location').value = filePaths[0];
      }
    }).catch(err => {
      console.log('err',err)
    })
  })

  $('#save-file-location-btn').addEventListener('click',() => {
    fileLocationSaveInStore($('#save-file-location').value)
  })
})